package com.ev.k3cloudBusiness.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ev.k3cloud.entity.K3CloudBillPushEntity;
import com.ev.k3cloud.entity.K3CloudBillSaveBaseEntity;
import com.ev.k3cloud.entity.K3CloudTopEntity;
import com.ev.k3cloud.entity.K3CloudViewEntity;
import com.ev.k3cloud.enums.K3CloudBillFormId;
import com.ev.k3cloud.service.K3CloudService;
import com.ev.k3cloud.vo.IdsVo;
import com.ev.k3cloudBusiness.service.K3OutStockDetailService;
import com.ev.k3cloudBusiness.service.K3ReceivableService;
import com.ev.k3cloudBusiness.service.K3SalOutStockService;
import com.ev.k3cloudBusiness.vo.K3SalOutStockEntryUpdateQtyVo;
import com.ev.k3cloudBusiness.vo.K3SalOutStockUpdateQtyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class K3SalOutStockServiceImpl implements K3SalOutStockService {
    @Autowired
    K3CloudService k3CloudService;

    @Autowired
    K3ReceivableService k3ReceivableService;

    @Autowired
    K3OutStockDetailService k3OutStockDetailService;

    @Override
    public String pushOutDetailByIds(IdsVo ids) {
        K3CloudBillPushEntity pushEntity = new K3CloudBillPushEntity();
        pushEntity.setIds(ids.getIds());
        pushEntity.setRuleId("46b04b7c-9c0d-430c-ab17-e5560a0ff233");
        K3CloudTopEntity topEntity = new K3CloudTopEntity();
        topEntity.setFormid(K3CloudBillFormId.SAL_STOCK_OUT.getFormId());
        topEntity.setData(pushEntity);
        return k3CloudService.push(JSON.toJSONString(topEntity));
    }

    @Override
    public String pushOutDetailByNumbers(String[] billNumbers) {
        K3CloudBillPushEntity pushEntity = new K3CloudBillPushEntity();
        pushEntity.setNumbers(billNumbers);
        pushEntity.setRuleId("46b04b7c-9c0d-430c-ab17-e5560a0ff233");
        K3CloudTopEntity topEntity = new K3CloudTopEntity();
        topEntity.setFormid(K3CloudBillFormId.SAL_STOCK_OUT.getFormId());
        topEntity.setData(pushEntity);
        return k3CloudService.push(JSON.toJSONString(topEntity));
    }

    @Override
    public String updateBillInfo(K3SalOutStockUpdateQtyVo param) {
        //设置客户收货时间为系统当前时间
        SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
        param.setF_XR_SUBDatetime(dateFormat.format(new Date()));

        K3CloudTopEntity topEntity = new K3CloudTopEntity();
        K3CloudBillSaveBaseEntity saveBaseEntity = new K3CloudBillSaveBaseEntity();
        saveBaseEntity.setIsDeleteEntry(false);
        saveBaseEntity.setModel(param);
        saveBaseEntity.setNeedUpDateFields(new String[] {"F_XR_SUBDatetime","FEntity","FPriceUnitQty"});
        topEntity.setFormid(K3CloudBillFormId.SAL_STOCK_OUT.getFormId());
        topEntity.setData(saveBaseEntity);
        return k3CloudService.save(JSON.toJSONString(topEntity));
    }

    @Override
    public Boolean validateReferenceByBillNo(String billNo) {
        Integer resultCount = 0;
        resultCount += k3ReceivableService.getBySourceBillNo(billNo);
        resultCount += k3OutStockDetailService.getBySourceBillNo(billNo);
        if (resultCount > 0) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public K3SalOutStockUpdateQtyVo getBillInfoByNo(String billNo) {
        K3CloudViewEntity viewEntity = new K3CloudViewEntity();
        viewEntity.setNumber(billNo);
        K3CloudTopEntity topEntity = new K3CloudTopEntity();
        topEntity.setFormid(K3CloudBillFormId.SAL_STOCK_OUT.getFormId());
        topEntity.setData(viewEntity);
        JSONObject resultObj = JSON.parseObject(k3CloudService.view(JSON.toJSONString(topEntity)));

        K3SalOutStockUpdateQtyVo k3SalOutStockUpdateQtyVo = new K3SalOutStockUpdateQtyVo();
        JSONObject dataObj = resultObj.getJSONObject("Result").getJSONObject("Result");
        k3SalOutStockUpdateQtyVo.setFid(dataObj.getLong("Id"));
        k3SalOutStockUpdateQtyVo.setBillNo(dataObj.getString("BillNo"));
        k3SalOutStockUpdateQtyVo.setBillDate(dataObj.getDate("Date"));
        k3SalOutStockUpdateQtyVo.setCustomerName(((JSONObject)(dataObj.getJSONObject("CustomerID").getJSONArray("Name")).get(0)).getString("Value"));
        k3SalOutStockUpdateQtyVo.setCarNo(dataObj.getString("FDriverTel"));
        JSONArray dataArray = dataObj.getJSONArray("SAL_OUTSTOCKENTRY");

        List<K3SalOutStockEntryUpdateQtyVo> detailList = new ArrayList<>();
        if (dataArray != null && dataArray.size() > 0) {
            for (Object detail : dataArray) {
                K3SalOutStockEntryUpdateQtyVo k3SalOutStockEntryUpdateQtyVo = new K3SalOutStockEntryUpdateQtyVo();
                k3SalOutStockEntryUpdateQtyVo.setFentryId(((JSONObject)detail).getLong("Id"));
                k3SalOutStockEntryUpdateQtyVo.setQty(((JSONObject)detail).getBigDecimal("BaseUnitQty"));
                k3SalOutStockEntryUpdateQtyVo.setFpriceUnitQty(((JSONObject)detail).getBigDecimal("BaseUnitQty"));
                k3SalOutStockEntryUpdateQtyVo.setMaterialName(((JSONObject)(((JSONObject)detail).getJSONObject("MaterialID").getJSONArray("Name").get(0))).getString("Value"));
                detailList.add(k3SalOutStockEntryUpdateQtyVo);
            }
        }
        k3SalOutStockUpdateQtyVo.setFentity(detailList);
        return k3SalOutStockUpdateQtyVo;
    }
}
